package com.xyz.auth.common;


import com.alibaba.fastjson.JSON;

import java.util.HashMap;

/**
 　* @Description: 统一返回数据
 　* @param
 　* @return
 　* @throws
 * @email nannan.kou@dtp1688.com
　 * @author kounannan
　 * @date 2018/5/29 9:42
　 */
public class R extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;

    public R() {
        put("code", 0);
        put("msg", "success");
    }

    public static R error() {
        return error(500, "未知异常，请联系管理员");
    }

    public static R error(String msg) {
        return error(500, msg);
    }

    public static R error(int code, String msg) {
        R r = new R();
        r.put("code", code);
        r.put("msg", msg);
        return r;
    }

    public static R ok(Object obj) {
        R r = new R();
        if(obj instanceof  String){
            r.put("msg", obj);
        }else{
            r.put("data",obj);
        }
        return r;
    }

    public R code(int code) {
        this.put("code", code);
        return this;
    }

    public static R ok() {
        return new R();
    }

    @Override
    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

    public boolean isSuccess() {
        return "0".equals(this.get("code").toString());
    }
}

